﻿// Copyright © 2010 Silvestru Iapascurta.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see license.txt for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Karoro
{
    public partial class PreferencesDialog : Form
    {
        public event EventHandler PreferencesChanged = delegate { };

        public PreferencesDialog()
        {
            InitializeComponent();
            this.themesComboBox.DisplayMember = this.themesComboBox.ValueMember = "Name";
        }

        public Object[] Themes
        {
            set
            {
                this.themesComboBox.DataSource = value;
            }
        }

        private void PreferencesDialog_Load(object sender, EventArgs e)
        {
            foreach (Style style in themesComboBox.Items)
            {
                if (style.Name == Properties.Settings.Default.style)
                {
                    themesComboBox.SelectedValue = Properties.Settings.Default.style;
                    break;
                }
            }

            if (themesComboBox.SelectedValue.ToString() != Properties.Settings.Default.style)
            {
                themesComboBox.SelectedValue = "Default";
            }            

            if (!Properties.Settings.Default.openInExternal)
                internalButton.Checked = true;
            else if (Properties.Settings.Default.openInExternal)
                externalButton.Checked = true;

            if (Properties.Settings.Default.showHumaneDates)
                showHumaneDates.Checked = true;

            if (Properties.Settings.Default.updateOnStartup)
                updateOnStartup.Checked = true;

            if (Properties.Settings.Default.showInTitle)
                showInTitlebar.Checked = true;

            timeFormatBox.Text = Properties.Settings.Default.timeFormat;
            itemsPerPage.Value = Properties.Settings.Default.itemsPerPage;
            connections.Value = Properties.Settings.Default.maxConnections;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            saveSettings();
            this.Close();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            saveSettings();
        }

        private void saveSettings()
        {
            Properties.Settings.Default.style = themesComboBox.SelectedValue.ToString();

            if (internalButton.Checked)
                Properties.Settings.Default.openInExternal = false;
            else
                Properties.Settings.Default.openInExternal = true;

            Properties.Settings.Default.showHumaneDates = showHumaneDates.Checked;
            Properties.Settings.Default.showInTitle = showInTitlebar.Checked;
            Properties.Settings.Default.updateOnStartup = updateOnStartup.Checked;

            Properties.Settings.Default.timeFormat = timeFormatBox.Text;
            Properties.Settings.Default.itemsPerPage = (int)itemsPerPage.Value;
            Properties.Settings.Default.maxConnections = (int)connections.Value;

            PreferencesChanged(this, null);
        }

    }
}
